package com.fhx.core.glide

import android.R
import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.text.TextUtils
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.SimpleTarget
import java.io.File


object ImgLoader {
    private const val SKIP_MEMORY_CACHE = false

    fun display(context: Context?, url: String?, imageView: ImageView?) {
        if (context == null || TextUtils.isEmpty(url)) {
            return
        }
        Glide.with(context).asDrawable().load(url)
            .skipMemoryCache(SKIP_MEMORY_CACHE)
            .optionalCenterCrop()
            .into(imageView!!)
    }

    @JvmStatic
    fun displayWithError(
        context: Context?,
        url: String?,
        imageView: ImageView?,
        errorRes: Int
    ) {
        if (context == null || TextUtils.isEmpty(url)) {
            return
        }
        Glide.with(context).asDrawable().load(url).error(errorRes)
            .skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView!!)
    }

    fun displayAvatar(
        context: Context?,
        url: String?,
        imageView: ImageView?
    ) {
        if (context == null || TextUtils.isEmpty(url)) {
            return
        }
        displayWithError(context, url, imageView, R.mipmap.sym_def_app_icon)
    }

    fun display(context: Context?, file: File?, imageView: ImageView?) {
        if (context == null) {
            return
        }
        Glide.with(context).asDrawable().load(file).skipMemoryCache(SKIP_MEMORY_CACHE)
            .into(imageView!!)
    }

    fun display(context: Context?, res: Int, imageView: ImageView?) {
        if (context == null) {
            return
        }
        Glide.with(context).asDrawable().load(res).skipMemoryCache(SKIP_MEMORY_CACHE)
            .into(imageView!!)
    }

    /**
     * 显示视频封面缩略图
     */
    fun displayVideoThumb(
        context: Context?,
        videoPath: String?,
        imageView: ImageView?
    ) {
        if (context == null || TextUtils.isEmpty(videoPath)) {
            return
        }
        Glide.with(context).asDrawable().load(Uri.fromFile(File(videoPath)))
            .skipMemoryCache(SKIP_MEMORY_CACHE).into(imageView!!)
    }

    fun displayDrawable(
        context: Context?,
        url: String?,
        callback: DrawableCallback?
    ) {
        if (context == null || TextUtils.isEmpty(url)) {
            return
        }
        Glide.with(context).asDrawable().load(url).skipMemoryCache(SKIP_MEMORY_CACHE)
            .into(object : SimpleTarget<Drawable?>() {
                override fun onLoadFailed(errorDrawable: Drawable?) {
                    callback?.onLoadFailed()
                }
                override fun onResourceReady(
                    resource: Drawable,
                    transition: com.bumptech.glide.request.transition.Transition<in Drawable?>?
                ) {
                    callback?.onLoadSuccess(resource)
                }
            })
    }

    fun clear(context: Context?, imageView: ImageView) {
        if (context == null) {
            return
        }
        Glide.with(context).clear(imageView)
    }

    /**
     * 显示圆角图片
     */
    fun displayGif(context: Context?, url: Int, imageView: ImageView?) {
        if (context == null) {
            return
        }
        Glide.with(context).load(url)
            .skipMemoryCache(SKIP_MEMORY_CACHE)
            .into(imageView!!)
    }

    /**
     * 显示圆角图片
     */
    fun displayRound(
        context: Context?,
        url: String?,
        imageView: ImageView?
    ) {
        if (context == null || TextUtils.isEmpty(url)) {
            return
        }
        Glide.with(context).asDrawable().load(url)
            .skipMemoryCache(SKIP_MEMORY_CACHE)
            .apply(RequestOptions.bitmapTransform(RoundedCorners(10)))
            .into(imageView!!)
    }

    interface DrawableCallback {
        fun onLoadSuccess(drawable: Drawable?)
        fun onLoadFailed()
    }
}